perf: O(children) directory lookups via reverse index#356
Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Draft
perf: O(children) directory lookups via reverse index#356Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Conversation
Replace O(total_mocks) full-hash scans in _files_in_dir() and the dir() constructor with O(children) lookups using a %_dir_children reverse index. The index maps each ancestor directory to the set of descendant paths registered under it. Maintained at mock registration (new, autovivify, broken-symlink creation), destruction (DESTROY), and rename re-keying — the same five mutation points that touch %files_being_mocked. For test suites with many mocked files, this eliminates per-readdir and per-rmdir linear scans of the entire mock registry. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replace O(total_mocks) linear scans with O(children) indexed lookups for directory-related operations.
Why
_files_in_dir()scans every key in%files_being_mockedon each call. It's used by readdir (contents()), thedir()constructor'shas_contentcheck, and the ENOTEMPTY checks inrmdirandrename. For test suites with many mocked files, this is a hot path paying unnecessary O(n) cost.How
New
%_dir_childrenreverse index maps each ancestor directory to its set of descendant mock paths. Maintained at the same 5 mutation points that already touch%files_being_mocked:new()constructor (registration)_create_file_through_broken_symlink()_maybe_autovivify()DESTROY()(cleanup)__rename()re-keying loopEach path registers under ALL ancestor directories (not just the immediate parent), preserving the existing semantics where
_files_in_dir('/a')returns descendants at any depth.Testing
Full test suite passes (94 files, 1586 subtests). Initial run caught a semantic mismatch (direct-parent-only vs all-ancestors) in globbing.t — fixed before push.
🤖 Generated with Claude Code